<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//include '../../php/dbconfig.php';
class CalendarUser
{
    function isAdminUser()
    {     
        $tmp_cookies=$_COOKIE;
        while(list($key,$val) = each($tmp_cookies)) {
	//echo "$key => $val<br />";
          $adminRoles = array('admin','editor');
          $uid= $this->selectUserFromSession($val,$adminRoles);
          if($uid>0)
          {
              return true;
          }
        }
        return false;
    }
    function isOnlyAdminUser()
    {     
        $tmp_cookies=$_COOKIE;
        while(list($key,$val) = each($tmp_cookies)) {
	//echo "$key => $val<br />";
          $adminRoles = array('admin');
          $uid= $this->selectUserFromSession($val,$adminRoles);
          if($uid>0)
          {
              return true;
          }
        }
        return false;
    }    
    //Check the role level more than  Authenticated role user, which include admin&editor role users
    function isAuthenticatedUser()
    {
        $tmp_cookies=$_COOKIE;
        while(list($key,$val) = each($tmp_cookies)) {
          $authenticatedRoles = array('admin','editor','authenticated user');
          $uid= $this->selectUserFromSession($val,$authenticatedRoles);
          if($uid>0)
          {
              return true;
          }
        }
        return false;
    }
    function selectUserFromSession($session_id,$roleTypes)
    { 
        $strSql="SELECT COUNT(1) as uid FROM `amcnn_sessions` s
        JOIN `amcnn_users` u ON s.`uid`=u.`uid`
        JOIN `amcnn_users_roles` ur ON u.`uid`=ur.`uid`
        JOIN `amcnn_role` r ON ur.`rid`=r.`rid`";
        
        if($roleTypes != NULL)
        {
            $strSql .=" AND (";
            $index = count($roleTypes);
             foreach ($roleTypes as $role) {
                $index --;
                if($index != 0)
                {
                    $strSql .= "r.`name`='".$role."' OR ";
                }
                else {
                    $strSql .= "r.`name`='".$role."'";
                }    
            }
            $strSql .=")";
        }
             
        $strSql .=" AND s.sid='".$session_id."';";
        //echo "<br>".$strSql;
        
        $this->OpenConnection();
        $handle=mysql_query($strSql);
        //echo "<br>".$handle;
        $row = mysql_fetch_object($handle);
        return $row->uid;
        //return 3;
    }
    function OpenConnection()
    {
	 $db = new DBConnection();
         $db->getConnection();
         
    }
    function getCurrentUserID()
    {
        $tmp_cookies=$_COOKIE;
         while(list($key,$val) = each($tmp_cookies)) {
          $uid= $this->selectUserIDFromSession($val);
          if($uid>0)
          {
              return $uid;
          }
        }
        return -1;
    }
    function selectUserIDFromSession($session_id)
    {
        //$session_id=100;//test
         $strSql="SELECT uid FROM   `amcnn_sessions` WHERE sid='".$session_id."'";
   
        $this->OpenConnection();
        $handle=mysql_query($strSql);
   
        $row = mysql_fetch_object($handle);
        if($row==false)
        {
            $err= mysql_error();
            return -1;
        }
        else
        {
            return $row->uid;
        }
    }
}
?>
